{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# An introduction to solving biological problems with Python\n",
    "\n",
    "## Session 2.2: Exercises and Modules\n",
    "\n",
    "- [Excercises 2.2.1](#Excercises-2.1.1)\n",
    "- [Excercises 2.2.2](#Excercises-2.2.2)\n",
    "- [Excercises 2.2.3](#Excercises-2.2.3)\n",
    "- [Modules](#Modules)\n",
    "- [Excercises 2.2.4](#Excercises-2.2.4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Excercises 2.2.1\n",
    "\n",
    "### Translate DNA sequence into protein sequence\n",
    "\n",
    "Write a function that translates a DNA sequence into a protein, a sequence of amino acids. The function should take 2 arguments, a DNA sequence and a dictionary that defines the standard genetic code.\n",
    "\n",
    "For mapping RNA codons to amino acids you can use the dictionary `standardGeneticCode` defined below. Notice that it only maps strings in upper case, so make sure that `codon` is in upper case before your look up. You can translate codon into an upper case with the `upper()` method on String. Notice also that it maps RNA codons and not DNA ones.\n",
    "\n",
    "First, loop over the sequence to extract every three basees until the end or until a stop codon either by using a `for` loop or a `while` one. \n",
    "\n",
    "Then convert the DNA into an RNA sequence, by replacing all T bases by U. Make sure that the codon corresponds to an amino accid. Convert the RNA codon into an amino acid using the dictionary provided and return the protein sequence as a list of amino acids."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "standardGeneticCode = { \n",
    "          'UUU':'Phe', 'UUC':'Phe', 'UCU':'Ser', 'UCC':'Ser',\n",
    "          'UAU':'Tyr', 'UAC':'Tyr', 'UGU':'Cys', 'UGC':'Cys',\n",
    "          'UUA':'Leu', 'UCA':'Ser', 'UAA': None, 'UGA': None,\n",
    "          'UUG':'Leu', 'UCG':'Ser', 'UAG': None, 'UGG':'Trp',\n",
    "          'CUU':'Leu', 'CUC':'Leu', 'CCU':'Pro', 'CCC':'Pro',\n",
    "          'CAU':'His', 'CAC':'His', 'CGU':'Arg', 'CGC':'Arg',\n",
    "          'CUA':'Leu', 'CUG':'Leu', 'CCA':'Pro', 'CCG':'Pro',\n",
    "          'CAA':'Gln', 'CAG':'Gln', 'CGA':'Arg', 'CGG':'Arg',\n",
    "          'AUU':'Ile', 'AUC':'Ile', 'ACU':'Thr', 'ACC':'Thr',\n",
    "          'AAU':'Asn', 'AAC':'Asn', 'AGU':'Ser', 'AGC':'Ser',\n",
    "          'AUA':'Ile', 'ACA':'Thr', 'AAA':'Lys', 'AGA':'Arg',\n",
    "          'AUG':'Met', 'ACG':'Thr', 'AAG':'Lys', 'AGG':'Arg',\n",
    "          'GUU':'Val', 'GUC':'Val', 'GCU':'Ala', 'GCC':'Ala',\n",
    "          'GAU':'Asp', 'GAC':'Asp', 'GGU':'Gly', 'GGC':'Gly',\n",
    "          'GUA':'Val', 'GUG':'Val', 'GCA':'Ala', 'GCG':'Ala', \n",
    "          'GAA':'Glu', 'GAG':'Glu', 'GGA':'Gly', 'GGG':'Gly'}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Excercises 2.2.2\n",
    "\n",
    "### Calculate the GC content of a DNA sequence\n",
    "\n",
    "Write a function that calculates the GC content of a DNA sequence by re-using the code written for the [Exercises 1.4.2](Introduction_to_python_day_1_session_4.ipynb#Exercises-1.4.2) yesterday."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Excercises 2.2.3\n",
    "\n",
    "### Extract the list of all overlaping sub-sequences\n",
    "Write a function that extracts a list of overlapping sub-sequences for a given window size from a given sequence. Do not forget to test it on a given DNA sequence."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Modules"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So far we have been writing Python code in files as executable scripts without knowning that they are also modules from which we are able to call the different functions defined in them.\n",
    "\n",
    "A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Create a file called `my_first_module.py` in the current directory with the following contents:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def say_hello(user):\n",
    "    print('hello', user, '!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now enter the Python interpreter from the directory you've created `my_first_module.py` file and import the `say_hello` function from this module with the following command:\n",
    "\n",
    "```bash\n",
    "python3\n",
    "Python 3.5.2 (default, Jun 30 2016, 18:10:25) \n",
    "[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>> from my_first_module import say_hello\n",
    ">>> say_hello('Anne')\n",
    "hello Anne !\n",
    ">>> \n",
    "```\n",
    "\n",
    "There is one module already stored in the course directory called `my_first_module.py`, if you wish to import it into this notebook, below is what you need to do. If you wish to edit this file and change the code or add another function, you will have to restart the notebook to have these changes taken into account using the restart the kernel button in the menu bar."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from my_first_module import say_hello\n",
    "say_hello('Anne')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A module can contain executable statements as well as function definitions. These statements are intended to initialize the module. They are executed only the first time the module name is encountered in an import statement. \n",
    "They are also run if the file is executed as a script.\n",
    "\n",
    "Do comment out these executable statements if you do not wish to have them executed when importing your module.\n",
    "\n",
    "For more information about modules, https://docs.python.org/3/tutorial/modules.html."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Excercises 2.2.4\n",
    "### Calculate GC content along the DNA sequence\n",
    "Combine the two methods written above to calculates the GC content of each overlapping sliding window along a DNA sequence from start to end. \n",
    "\n",
    "From the two files you wrote, import the methods written at exercices 2.2.2 and 2.2.3.\n",
    "The new function should take two arguments, the DNA sequence and the size of the sliding window, and re-use the previous methods written to calculate the GC content of a DNA sequence and to extract the list of all overlapping sub-sequences. It returns a list of GC% along the DNA sequence."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next session\n",
    "\n",
    "Go to our next notebook: [Introduction_to_python_day_2_session_3](Introduction_to_python_day_2_session_3.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
